package com.mrobuy.jd.brand;

import com.common.utils.AccountContextHolder;
import com.gavel.database.SQLExecutor;
import com.gavel.utils.StringUtils;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jd.api.Jos;
import com.mrobuy.jdvc.entity.VCBrand;
import com.mrobuy.util.MD5Utils;

import java.util.List;

public class VCBrandLoader {


    public static void main(String[] args) throws Exception {


        load(AccountContextHolder.vendorCode(), JdClientFactory.jdClient());
    }

    public static void load(String vendor, JdClient client) throws Exception {


        List<VCBrand> vcBrandList = Jos.brandFindClient(client).call();

        for (VCBrand vcBrand : vcBrandList) {
            vcBrand.setVendor(vendor);
            if (StringUtils.isBlank(vcBrand.getId())) {
                vcBrand.setId(MD5Utils.md5Hex(vendor + "_" + vcBrand.getBrandid()).trim().toUpperCase());
            }


            VCBrand exist = SQLExecutor.executeQueryBean("select * from VC_BRAND where BRANDID = ? and VENDOR = ? ", VCBrand.class,  vcBrand.getBrandid(), vendor);
            if ( exist!=null ) {
                continue;
            }

            System.out.println(new Gson().toJson(vcBrand));

            SQLExecutor.insert(vcBrand);

        }

    }


}
